﻿using System;
using ReAnimation.Helpers.IoC;
using ReAnimation.MVP.ModelInterfaces;
using ReAnimation.MVP.ViewInterfaces;

namespace ReAnimation.MVP.Presenters
{
    public class MainPresenter : BasePresenter<IMainView>
    {
        private readonly IMainModel _mainModel;
        private readonly ILazyInject<ILoginView> _loginDialogInjector;
        private readonly ILazyInject<IChooseMoleculeView> _chooseMoleculeViewInjector;
        private readonly ILazyInject<INewMoleculeDialogView> _newMoleculeDialogView;

        protected override void InitView()
        {
            _view.LoadFromDb += LoadFromDbHandler;
            _view.LinkingAtomsPairSelected += _view_LinkingAtomsPairSelected;
            _view.CreateNewMolecule += ViewOnCreateNewMolecule;
        }

        private void ViewOnCreateNewMolecule(object sender, EventArgs eventArgs)
        {
            _newMoleculeDialogView.Get().ShowViewDialog();
        }

        void _view_LinkingAtomsPairSelected(object sender, EventArgs e)
        {
            _view.ShowAtomsLinkingDialog();
        }

        public MainPresenter(IMainModel mainModel, 
                             ILazyInject<ILoginView> loginDialogInjector, 
                             ILazyInject<IChooseMoleculeView> chooseMoleculeViewInjector,
                             ILazyInject<INewMoleculeDialogView> newMoleculeDialogView)
        {

            _mainModel = mainModel;
            _newMoleculeDialogView = newMoleculeDialogView;
            _chooseMoleculeViewInjector = chooseMoleculeViewInjector;
            _loginDialogInjector = loginDialogInjector;
            _mainModel.CurrentMoleculeChanged += currentMoleculeChangedHandler;
        }

        void LoadFromDbHandler(object sender, EventArgs e)
        {/*
            ILoginView loginDialog = _loginDialogInjector.Get();
            if (loginDialog.ShowDialog() == DialogResult.OK)
            {
                IChooseMoleculeView chooseMoleculeView = _chooseMoleculeViewInjector.Get();
                chooseMoleculeView.ShowDialog();
            }*/
        }

        private void currentMoleculeChangedHandler(object sender, EventArgs e)
        {

        }

    }
}